<!DOCTYPE html>
<html>
  <head>
  	<div th:include="base-libs :: base-libs" />
    <title>床位设置-新增/修改</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
    <style>
    	#yjTable .yyyxjTr td{
    		/* background:#fff;
    		border:0; */
    	}
    	.spanBtn{
    		color:blue;
    		cursor:pointer;
    		margin-left: 8px;
    	}
    	.deleteBtn{
    		color:red;
    		cursor:pointer;
    		margin-left: 8px;
    	}
    	.hidden{
    		display:none;
    	}
    	input{
    		padding-left:4px;
    	}
    	select{
    		width:91.6% !important;
    	}
    	.combo{
    		width:91% !important
    	}
    </style>
    
    <script th:inline="javascript">
	    /**读取request中的初始化数据*/
	    var requestData = [[${requestData}]];
	</script>
	<script>
		$(function(){
            $(this).registerManagerPage();
			initView();
			initData();
		})
		
		function initView(){
			//保存
			$("#btnBc").click(function(){
				if($(".form-control").QuickValidate()){
					var yyyxjArr = [];//预约优先级
					$(".yyyxjTr input").each(function(){
						var yyyxjObj = {
							YYLBBM: $(this).attr('YYLBBM'),
							XH: $(this).val()
						}
						yyyxjArr.push(yyyxjObj);
					})
					
					var params = {
						yyyxj: JSON.stringify(yyyxjArr),
						XZYH: parent.userInfo.USERID
					}
					$.QuickRemote.AjaxFiledFormSubmit("cwszForm", saveHandler, params);
				}
			})
			
			//返回
			$("#btnFh").click(function(){
				$.QuickUrlUtils.redirectUrl("index.html");
			})
			
			//一级科室代码
			$("#YJKSDM").change(function(){
				var optionText = $(this).children(':selected').text();
				if(optionText=='请选择') optionText = '';
				$("#YJKSMC").val(optionText);
			})
			
			//病区代码--指定病区名称
			$("#BQDM").change(function(){
				var optionText = $(this).children(':selected').text();
				if(optionText=='请选择') optionText = '';
				$("#BQMC").val(optionText);
			})
			
			//床位保留时间
			$("#day,#hours").keyup(function(){
				var day = parseInt($("#day").val()),
					hours=parseInt($("#hours").val());
				
				var CWBLSJ;
				if(day>=0 && hours>=0){
					CWBLSJ = day*24 + hours;
				}
				$("#CWBLSJ").val(CWBLSJ);
			})

            //查询科室病区
            $("#YJKSDM").change(function(){
                var YLJGDM = $("#YLJGDM").val();
                var YJKSDM = $("#YJKSDM").val();
                initBqxx({YLJGDM:YLJGDM,YJKSDM:YJKSDM});
            })
			
			/** 添加优先级类别  */
			$("#btnTjyxjlb").click(function(){
				var selectedYylbData = [];
				$(".yyyxjTr input").each(function(){
					selectedYylbData.push($(this).attr('YYLBBM'));
				})
				if(selectedYylbData.length==4){
					$.QuickAlert.alertFail({content: '没有更多类别了！'});
				}else{
					StoreCache.setCache('selectedYylbData',selectedYylbData);
					parent.layer.open({
						type : 2,
						title : "选择预约优先级类别",
						shadeClose : false,
						shade : 0.5,
						area : [ "250px", "150px" ],
						content: "/" + contextRoot + 'cwsz/yyyxjlb.html',
						end: function(){
							var yylbData = StoreCache.removeCache('yylbData');
							if(!$.isEmptyObject(yylbData)){
								var yyyxjTrBox = $($("#yyyxjTrBox").html());
								yyyxjTrBox.find('.YYLBBM').attr("YYLBBM", yylbData.YYLBBM);
								yyyxjTrBox.find('.YYLBMC').html(yylbData.YYLBMC);
								$("#yjTable").append(yyyxjTrBox);
								yyyxjPx();
							}
						}
					}); 
				}
			})
			
			/** 预约优先级上移/下移 */
			$("body").on('click', '.yyyxjTr .spanBtn', function(){
				var type = $(this).attr('type'); //up/down
				var parentTr = $(this).parents('tr');
				var parentTr_prev = parentTr.prev(),
					parentTr_next = parentTr.next();
				parentTr.next(parentTr_prev);
				if(type=='up' && parentTr_prev.hasClass('yyyxjTr')){
					parentTr.after(parentTr_prev);
				}else if(type=='down' && parentTr_next.hasClass('yyyxjTr')){
					parentTr_next.after(parentTr);
				}
				yyyxjPx();
			})
			/** 删除优先级类别 */
			$("body").on('click', '.deleteBtn', function(){
				$(this).parents('tr').remove();
				yyyxjPx();
			})
			
			/** 医疗机构combogrid */
			var readonly = !requestData["isManager"];
			$("#combogridYljg").combogrid({
	            height: 30
	            ,width: 300
	            ,panelWidth: 500
	            ,singleSelect: true
	            ,pagination: true
	            ,idField: 'PTJGID'
	            ,textField: 'YLJGMC'
	            ,mode: "remote"
	            ,readonly: readonly
	            ,hasDownArrow: !readonly
	            ,url: "/console/yljgxx/queryPageSummary.service"
	            ,delay: 1000
	            ,reversed: false
	            ,columns:[[
	                {field:'YLJGMC',title:'机构名称',width:240},
	                {field:'XZQHMC',title:'行政区划',width:80},
	                {field:'WSJGLBMC',title:'机构类别',width:80},
	                {field:'YYDJMC',title:'机构等级',width:80}
	            ]]
	            ,onBeforeLoad: function(queryParams) {
	                var url = $(this).datagrid("options")["url"];
					/*URL不等空时再发送请求*/
	                if (!readonly) {
	                    queryParams["pageSize"] = queryParams["rows"];
	                    queryParams["pageNumber"] = queryParams["page"];
	                    queryParams["YLJGMC"] = queryParams["q"];
	                    var result = [];
	                    $("#YJKSDM").createOptions(result,{textField : "KSMC" , valueField : "KSDM"});
	                    return true;
	                }else{
	                    return false;
	                }
	            },
	            onClickRow : function(rowIndex, rowData){
	                initKsxx(rowData);
	            },
	            onLoadSuccess : function(data){

				}
	        });
			
		}
		
		function initData(){
			var cwszData = StoreCache.removeCache('cwszData');
			if(!$.isEmptyObject(cwszData)){
	            $("#combogridYljg").combogrid("setValue", cwszData);
	            initKsxx(cwszData);
	            
				$("#type").val('update');
				$(".form-control").each(function(){
					var name = $(this).attr('name');
					$(this).val(cwszData[name]);
				})
				//床位保留时间格式化为：天+小时
				formatHoursToDetail(cwszData.CWBLSJ);
				
				$.QuickRemote.AjaxJson("queryCwyyyxj", function(result){
					if(result.state){
						var data = result.data;
						var yylbObj = {'qianYue':'签约','shuangZhuan':'双转','menZhen':'门诊','zhuYuan':'住院',};
						$.each(data, function(i, yylbData){
							var yyyxjTrBox = $($("#yyyxjTrBox").html());
							yyyxjTrBox.find('.YYLBBM').attr("YYLBBM", yylbData.YYLBBM);
							yyyxjTrBox.find('.YYLBMC').html(yylbObj[yylbData.YYLBBM]);
							$("#yjTable").append(yyyxjTrBox);
						})
						yyyxjPx();
					}else{
						$.QuickAlert.alertFail({content: result.info});
					}
		        }, {CWSZBH: cwszData.CWSZBH});
			}else if(!requestData["isManager"]){
			    var jgxx = {PTJGID: parent.userInfo.PTJGID, YLJGMC: parent.userInfo.YLJGMC};
                $("#combogridYljg").combogrid("setValue", requestData);
                initKsxx(requestData);
			}
			$("#day").trigger("keyup");
		}
		
		/** 保存回调 */
		function saveHandler(result){
			if(result.state){
				$.QuickAlert.alertNormal(result.info, {callback: function(){
					$("#btnFh").trigger("click");
				}});
			}else{
				$.QuickAlert.alertFail({content: result.info});
			}
		}
		
		/** 将小时转为：天+小时 */
		function formatHoursToDetail(hours){
			hours = parseInt(hours);
			$("#hours").val(hours%24);
			$("#day").val((hours-hours%24)/24);
		}
		
		/** 预约优先级排序 */
		function yyyxjPx(){
			$(".yyyxjTr input").each(function(i){
				$(this).val(i+1);
			})
		}
		
		/** 初始化一级科室下拉框 */
		function initKsxx(params2){
			var params = $.extend({},params2);//处理乱码
			$("#YLJGDM").val(params.YLJGDM);
			$("#YLJGMC").val(params.YLJGMC);
			$("#PTJGID").val(params.PTJGID);
			$("#YYDJMC").val(params.YYDJMC);
	        $.QuickRemote.AjaxJson("/console/ksxx/", function(result){
	            $("#YJKSDM").createOptions(result,{textField : "KSMC" , valueField : "KSDM"});
	            $("#YJKSDM").val(params.YJKSDM);
                initBqxx(params);
	        }, params);
		}

        /** 初始化病区下拉框 */
        function initBqxx(params){
            $.QuickRemote.AjaxJson( "/" + contextRoot + "cwyygl/queryBqxxByPtjgid", function(result){
                $("#BQDM").createOptions(result,{textField : "BQMC" , valueField : "BQDM"});
                $("#BQDM").val(params.BQDM);
            }, {YLJGDM: params.YLJGDM, KSDM: params.YJKSDM});
        }
	</script>
  </head>
  
  <body class="easyui-layout">
    <div region="center">
    	<h4 class="h4-title" style="text-align:center;margin:0;padding:0;font-size: 16px;height: 30px;line-height: 30px;">
			医院床位设置
		</h4>
		<form action="save" id="cwszForm">
			<input id="type" name="type" class="hidden" value="add"/>
			<input name="CWSZBH" class="form-control hidden"/>
			<div class="easyui-panel" title="基本信息" border="false">
		    	<table class="form-table">
					<tr>
						<td class="td-text" style="width:20%;"><label>医院名称</label></td>
						<td class="td-content" style="width:30%;">
							<input id="combogridYljg" />
						
							<input id="YLJGMC" name="YLJGMC" class="form-control hidden" 
							quick-config="label:'医院名称', validateProxy:'notNull'"/>
							<input id="PTJGID" name="PTJGID" class="form-control hidden"/>
							<input id="YLJGDM" name="YLJGDM" class="form-control hidden"/>
							<input id="YYDJMC" name="YYDJMC" class="form-control hidden"/>
						</td>
						<td class="td-text" style="width:20%;"><label>科室名称</label></td>
						<td class="td-content">
							<select id="YJKSDM" name="YJKSDM" class="form-control" quick-config="label:'科室名称', validateProxy:'notNull'">
								<option value="">请选择</option>
							</select>
							<input id="YJKSMC" name="YJKSMC" class="form-control hidden"/>
						</td>
					</tr>
					<tr>
						<td class="td-text"><label>病区名称</label></td>
						<td class="td-content">
							<select id="BQDM" name="BQDM" class="form-control" quick-config="label:'病区名称', validateProxy:'notNull'">
								<option value="">请选择</option>
							</select>
							<input id="BQMC" name="BQMC" class="form-control hidden" />
						</td>
						<td class="td-text"><label>是否启用床位预约</label></td>
						<td class="td-content">
							<select name="SFQYCW" class="form-control">
								<option value="1">是</option>
								<option value="0">否</option>
							</select>
						</td>
					</tr>
					<tr>
						<td class="td-text"><label>是否需审核</label></td>
						<td class="td-content">
							<select name="SFXYSH" class="form-control">
								<option value="1">是</option>
								<option value="0">否</option>
							</select>
						</td>
					</tr>
				</table>
			</div>
			
			<div class="easyui-panel" title="床位统一设置" border="false">
				<table class="form-table">
					<tr>
						<td class="td-text" style="width:20%;"><label>可预约床位数(%)</label></td>
						<td class="td-content" style="width:30%;">
							<input name="KYYCWS" class="form-control" 
							quick-config="label:'可预约床位数', validateProxy:'notNull length num', length: '3'"/>
						</td>
						<td class="td-text" style="width:20%;"><label>3天内可预约床位数</label></td>
						<td class="td-content">
							<input name="STNKYYCWS" class="form-control" 
							quick-config="label:'3天内可预约床位数', validateProxy:'notNull length num', length: '3'"/>
						</td>
					</tr>
					<tr>
						<td class="td-text"><label>一周内可预约床位数</label></td>
						<td class="td-content">
							<input name="YZNKYYCWS" class="form-control" 
							quick-config="label:'一周内可预约床位数', validateProxy:'notNull length num', length: '3'"/>
						</td>
					</tr>
				</table>
			</div>
			
			<div class="easyui-panel" title="床位预警值设置" border="false">
				<table class="form-table" id="yjTable">
					<tr>
						<td class="td-text" style="width:20%;"><label>床位保留时间设置</label></td>
						<td class="td-content" style="width:30%;">
							<input id="day" class="form-control" style="width:20%;margin-right:5px" 
							quick-config="label:'天数', validateProxy:'notNull num'" value="0"/>天
							<input id="hours" class="form-control" style="width:20%;margin-left:15px;margin-right:5px" 
							quick-config="label:'小时数', validateProxy:'notNull num'" value="0"/>小时
							
							<input id="CWBLSJ" name="CWBLSJ" class="form-control hidden" 
							quick-config="label:'床位保留时间设置', validateProxy:'notNull length num', length: '10'"/>
						</td>
						<td class="td-text" style="width:20%"></td>
						<td class="td-text"></td>
					</tr>
					<tr>
						<td class="td-text">预约优先级设置</td>
						<td class="td-text" style="text-align:left">类别</td>
						<td class="td-text"><label>排序</label></td>
						<td class="td-text" style="text-align:left;position:relative">
							<span class="spanBtn" id="btnTjyxjlb">添加</span>
						</td>
					</tr>
				</table>
			</div>
		</form>
		
    </div>
    
    <div region="south" style="height:50px;line-height:50px;text-align:center;overflow:hidden;border:0;border-top:1px solid #ddd">
    	<button id="btnBc" class="btn btn-info" type="button">保存</button>
    	<button id="btnFh" class="btn btn-info" type="button">返回</button>
    </div>
    
  </body>
  
  <script type="text/html" id="yyyxjTrBox">
		<tr class="yyyxjTr"><!-- 预约优先级Tr -->
				<td class="td-text"><label></label></td>
				<td class="td-content YYLBMC">签约</td>
				<td class="td-content">
					<input class="YYLBBM" style="width:40px;float:right" value="1" YYLBBM="xxx" readonly="readonly"/> <!-- YYLBBM:预约类别编码 -->
				</td>
				<td class="td-content">
					<span class="deleteBtn">删除</span>
					<span class="spanBtn" type="up">上移</span>
					<span class="spanBtn" type="down">下移</span>
				</td>
		</tr>
  </script>
</html>
